<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../../layuimini-2/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../layuimini-2/css/layuimini.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">房间号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="roomNum" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">物品类型</label>
                            <div class="layui-input-block">
<!--                                 <input type="text" name="goodsType" autocomplete="off" class="layui-input">-->
                                <select id="goodsType" name="goodsType" lay-verify="required" >
                                    <option value="0">请选择</option>
                                </select>

                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">物品价格</label>
                            <div class="layui-input-inline">
                                <input type="text" name="price" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="deleteBatch"> 批量删除 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="export"> 导出 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="import"> 导入 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="templatedownload"> 模板下载 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script src="../../../layuimini-2/lib/layui-v2.6.3/layui.js" charset="UTF-8"></script>
<script>
    var $ ;
    var form;
    var table;
    layui.use(['jquery','form', 'table'], function () {
        $ = layui.jquery,
        form = layui.form,
        table = layui.table;


        $.ajax({
            url: '/goods/select.do',
            dataType: 'json',
            type: 'get',
            success: function (data) {
                console.log(data);//下面会提到这个data是什么值
                //使用循环遍历，给下拉列表赋值
                $.each(data.data, function (index, value) {//传入数组,不是对象
                    console.log(value);
                    $('#goodsType').append(new Option(value, value));// 下拉菜单里添加元素

                });
                layui.form.render("select");//重新渲染 固定写法

            },
            end: function () {
                table.reload("like")
            },
            async: false//不异步,异步锁

        })

        // 渲染表格
        table.render({
            elem: '#currentTableId',
            url: '/goods/list.do',
            toolbar: '#toolbarDemo',
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'id', width: 80, title: 'ID', sort: true},
                {field: 'roomNum', title: '房间号'},
                {field: 'goodsType', title: '物品类型',},
                {field: 'price', title: '物品价格'},
                {field: 'num', title: '物品数量',},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (obj) {

            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where:obj.field
            }, 'data');

            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加用户',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/view/roomManagement/goods/goodsAdd.html',
                    end: function () {
                        table.reload("currentTableId")
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'deleteBatch') {  // 监听批量删除操作
                var checkStatus = table.checkStatus("currentTableId");
                if (checkStatus.data.length !=0) {
                    var arr = [];
                    for (var i=0;i < checkStatus.data.length;i++) {
                        arr.push(checkStatus.data[i].id);
                    }
                    $.ajax({
                        url: "/goods/delete/batch.do",
                        data:{ids:arr},
                        type:"get",
                        traditional:true,
                        success: function (ret) {
                            if (ret.code==0) {
                                layer.msg("删除成功!")
                                table.reload("currentTableId");
                            }
                        },
                        error:function () {
                            layer.msg("服务器忙!");
                        }
                    })
                } else {
                    layer.alert("请选择需要删除的数据",{icon:0,title:'提示'} )
                }
            } else if (obj.event === 'export') {  // 监听数据表导出操作
                location.href="/goods/export.do?\"+$(\"#fm1\").serialize();";
            }
            else if (obj.event === 'import') {  // 监听数据表导入操作
                layer.open({
                    title: '货物信息导入',
                    shade: 0.2,
                    type: 2,
                    shadeClose: true,   // 点别的地方 是否关闭当前 layer    true收关闭   false 不关闭
                    area: ['50%', '80%'],
                    fixed: true, //不固定
                    maxmin: true,
                    content: '/view/roomManagement/goods/goodsImport.html',
                    end: function (index) {
                        table.reload('currentTableId');
                    }
                });
            }else if (obj.event === 'templatedownload') {  // 监听数据表模板下载操作
                location.href="/goods/templateDownload.do";
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            if (obj.event === 'edit') {
                var index = layer.open({
                    title: '编辑用户',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/view/roomManagement/goods/goodsEdit.html',
                    success: function (layero, index) {//弹出层open方法
                        let body = layer.getChildFrame("body", index);
                        let feastData = obj.data;
                        body.find("input[name=id]").val(feastData.id)
                        body.find("input[name=roomNum]").val(feastData.roomNum)
                        body.find("input[name=goodsType]").val(feastData.goodsType)
                        body.find("input[name=price]").val(feastData.price)
                        body.find("input[name=num]").val(feastData.num)
                    },
                    end: function () {
                        table.reload("currentTableId")
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm("确认删除", {icon: 3, title: "提示"}, function (index) {
                    $.get("/goods/delete.do", {id: obj.data.id}, function (ret) {
                        if (ret.code == 0) {
                            layer.msg("删除成功")
                        } else {
                            layer.msg("删除失败")
                        }
                    });
                    layer.close(index);
                    table.reload("currentTableId")
                }, function (index) {
                    layer.msg("取消删除")
                    layer.close(index)
                });
            }
        });


    });
</script>

</body>
</html>